//
//  UserLivingDetailViewController.m
//  TestYoho
//
//  Created by mac on 17/2/22.
//  Copyright © 2017年 YiDarren. All rights reserved.
//

#import "UserLivingDetailViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVKit/AVKit.h>


#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)


@interface UserLivingDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (strong,nonatomic) MPMoviePlayerController *player;

@end

@implementation UserLivingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupVideoPlayer];
}

- (void)setupVideoPlayer {
    
    self.player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://vedio.ssrj.cn/static/E8051747.mp4"]];
    self.player.shouldAutoplay = NO;
    [self.player.view setFrame:self.view.bounds];
    [self.player setControlStyle:MPMovieControlStyleNone];
    self.player.repeatMode = MPMovieRepeatModeNone;
    [self.player prepareToPlay];
}

-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return SCREEN_HEIGHT;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserLivingDetailTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UserLivingDetailTabelViewCell"];
    if (cell == nil) {
        
        cell = [[UserLivingDetailTabelViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UserLivingDetailTabelViewCell"];
    }
    cell.bgImageView.image = [UIImage imageNamed:@"ykbg2"];
    [cell.bgImageView addSubview:self.player.view];
    cell.dismissBtn.tag = indexPath.row;
    [cell.dismissBtn addTarget:self action:@selector(dismissBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    UserLivingDetailTabelViewCell *userCell = (UserLivingDetailTabelViewCell *)cell;
//    [self.player stop];
//    [userCell.player stop];
    
    NSLog(@"1111");
    
    if (self.player) {
        
        
    }

 
}

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"2222");
    
    [self.player play];
    
}


- (void)dismissBtnAction:(UIButton *)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

int i = 1;
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    i++;
    
    if (i%2 == 0) {
        
        [self.player play];

    }
    else {
        
        [self.player pause];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end



@implementation UserLivingDetailTabelViewCell
int n = 0;
-(void)prepareForReuse {
    n++;
    NSLog(@"aaaaaa %d",n);
    self.bgImageView = nil;
    
    [self.player stop];
    
}

-(void)awakeFromNib {
    [super awakeFromNib];
}

@end


